package com.jasmine.auth.controller;

import com.jasmine.auth.TokenManager;
import com.jasmine.auth.model.Token;
import com.jasmine.core.constants.BjsConstants;
import com.jasmine.core.dto.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author jasmineXz
 */
@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private TokenManager tokenManager;


    /**
     * 校验token
     * @param tokenStr token
     * @return token信息 {@link Token}
     */
    @GetMapping("/access")
    public R getToken (@RequestHeader(BjsConstants.HTTP_HEADER_AUTHORIZATION)String tokenStr) {
        return R.ok(tokenManager.getAccessToken(tokenStr));
    }


    /**
     * 重置token过期时间为默认值
     * @param tokenStr token
     */
    @GetMapping("/access/reset")
    public R resetToken (@RequestHeader(BjsConstants.HTTP_HEADER_AUTHORIZATION)String tokenStr) {
        tokenManager.resetToken(tokenStr);
        return R.ok();
    }


    /**
     * 刷新token
     * @param tokenStr token
     * @return 新token
     */
    @PostMapping("/refresh")
    public R refresh (@RequestHeader(BjsConstants.HTTP_HEADER_AUTHORIZATION)String tokenStr) {
        return R.ok(tokenManager.refresh(tokenStr));
    }


    /**
     * 通过token 获取 刷新token
     * @param tokenStr token
     * @return 刷新token
     */
    @Deprecated
    @GetMapping("/refresh")
    public R getRefresh (@RequestHeader(BjsConstants.HTTP_HEADER_AUTHORIZATION)String tokenStr) {
        Token token = tokenManager.getRefreshToken(tokenStr);
        token.setAccessToken(null);
        token.setAccessExpires(null);
        token.setUserInfo(null);
        return R.ok(token);
    }



}
